#ifndef __EPOLL_HPP__
#define __EPOLL_HPP__

#include <iostream>
#include <sys/epoll.h>

class Epoll
{
private:
    static const int g_size=256;
    static const int g_timeout=5000;

public:
    Epoll(int timeout=g_timeout)
        :_timeout(timeout)
    {}

    int EpollCreate()
    {
        _epfd=epoll_create(g_size);
        return _epfd;
    }

    bool AddEpollEvent(int sock,uint32_t events)
    {
        struct epoll_event ev;
        memset(&ev,0,sizeof(ev));
        ev.events=events;
        ev.data.fd=sock;
        if(epoll_ctl(_epfd,EPOLL_CTL_ADD,sock,&ev)<0) return false;
        return true;
    }

    int EpollWait(struct epoll_event events[],int evevts_num)
    {
        int n=epoll_wait(_epfd,events,evevts_num,_timeout);
        return n;
    }

    bool EpollCtl(int sock,uint32_t events)
    {
        events|=EPOLLET;
        struct epoll_event ev;
        memset(&ev,0,sizeof(ev));
        ev.events=events;
        ev.data.fd=sock;
        int n=epoll_ctl(_epfd,EPOLL_CTL_MOD,sock,&ev);
        return n==0;
    }

    bool EpollDel(int sock)
    {
        int n=epoll_ctl(_epfd,EPOLL_CTL_DEL,sock,nullptr);
        return n==0;
    }

private:
    int _epfd;
    int _timeout;
};

#endif